﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Data.SqlClient;
using System.Configuration;

using ECS.Framework;
using ECS.BLL;
using ECS.BLL.Component;
using ECS.BLL.Entity;

namespace E_ICS.E_ICS
{
    public partial class frmAgentSearch : Form
    {
        #region private properties
        string AgentCode, AgentName, Branch, Address, City, ews_required;
        string SelAgentCode, SelAgentName, SelEws;
        DataSet dataset;
        Main.frmMain2 main;
        //string appId = ConfigurationManager.AppSettings["appID"];
        //string device = ConfigurationManager.AppSettings["device"];
        
        //SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["con3"].ConnectionString);
        
        #endregion

        #region form events

        public frmAgentSearch(Main.frmMain2 _main)
        {
            InitializeComponent();
            main = _main;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                SearchAgent();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                // string[] array = new string[3] { SelAgentCode, SelAgentName, SelEws };
                ValueTofrmAddMAWB.SelAgentCode = SelAgentCode;
                ValueTofrmAddMAWB.SelAgentName = SelAgentName;
                ValueTofrmAddMAWB.SelEws = SelEws;
                if (ViewEditWeightSlip.flagfromViewEditWeightSlip == 1)
                {
                    this.Dispose();
                    this.DialogResult = DialogResult.Yes;
                    SelAgentCode = "";
                    SelAgentName = "";
                    SelEws = "";
                }
                else
                {
                    //main.GetDataFromAgentSearch(array);
                    // frmEditMAWBInfo fm = new frmEditMAWBInfo(main);
                    //fm.GetDataFromAgentSearch(array);
                    //fm.ShowDialog();
                    this.Dispose();
                    this.DialogResult = DialogResult.Yes;
                    SelAgentCode = "";
                    SelAgentName = "";
                    SelEws = "";
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = e.RowIndex;
                if (e.RowIndex >= 0)
                {
                    //dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    dgvAgent.Rows[index].Selected = true;
                    if (dgvAgent.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null && e.RowIndex != -1)
                    {
                        SelAgentCode = dgvAgent.Rows[e.RowIndex].Cells[0].Value.ToString();
                        SelAgentName = dgvAgent.Rows[e.RowIndex].Cells[1].Value.ToString();
                        SelEws = dgvAgent.Rows[e.RowIndex].Cells[5].Value.ToString();
                        ViewEditWeightSlip.Agent = SelAgentCode;
                        ViewEditWeightSlip.Agent_Name = SelAgentName;

                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
                //Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
            }
        }
        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                dgvAgent.ClearSelection();
                SelAgentCode = "";
                SelAgentName = "";
                SelEws = "";
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                dgvAgent.DataSource = null;
                txtAgentCode.Text = "";
                txtAgentName.Text = "";
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }

        }
        private void txtAgentCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    SearchAgent();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
            }
        }

        private void txtAgentName_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    SearchAgent();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
            }

        }
        #endregion

        #region private methods

        private DataTable CreateAgent_TB(DataSet ds)
        {
            DataTable TW = new DataTable();
            ///////////create head column/////////////////
            TW.Columns.Add("Agent Code", typeof(string));
            TW.Columns.Add("Agent Name", typeof(string));
            TW.Columns.Add("Branch", typeof(string));
            TW.Columns.Add("Address", typeof(string));
            TW.Columns.Add("City", typeof(string));
            TW.Columns.Add("ews_required", typeof(string));
            ///////////////////////////////////////////////
            int row = ds.Tables[0].Rows.Count;
            for (int i = 0; i < row; i++)
            {
                AgentCode = ds.Tables[0].Rows[i]["agent_code"].ToString().Trim();
                AgentName = ds.Tables[0].Rows[i]["agent_name"].ToString().Trim();
                Branch = ds.Tables[0].Rows[i]["branch"].ToString().Trim();
                Address = ds.Tables[0].Rows[i]["address"].ToString().Trim();
                City = ds.Tables[0].Rows[i]["city"].ToString().Trim();
                ews_required = ds.Tables[0].Rows[i]["ews_required"].ToString().Trim();
                TW.Rows.Add(AgentCode, AgentName, Branch, Address, City, ews_required);
            }
            return TW;
        }
        /// <summary>
        /// search agent with Code AND/OR Name
        /// </summary>
        private void SearchAgent()
        {
            dgvAgent.DataSource = null;
            SearchInfo search = new SearchInfo();
            SearchCriteria criteria = new SearchCriteria();
            criteria.AppId = GlobalVariable.user.ApplicationID;
            criteria.UserId = GlobalVariable.user.UserID;
            criteria.Device = GlobalVariable.user.Device;
            criteria.AgentCode = txtAgentCode.Text.Trim();
            criteria.AgentName = txtAgentName.Text.Trim();
            dataset = new DataSet();
            dataset = search.AgentSearch(criteria);
            if (dataset.Tables[0].Rows.Count > 0)
            {
                string status = dataset.Tables[0].Rows[0]["Status"].ToString();
                string message = dataset.Tables[0].Rows[0]["Message"].ToString();
                if (status == "PASS")
                {
                    DataTable Agent_TB = CreateAgent_TB(dataset);
                    dgvAgent.DataSource = Agent_TB;
                    dgvAgent.Rows[0].Selected = true;
                    SelAgentCode = dgvAgent.Rows[0].Cells[0].Value.ToString();
                    SelAgentName = dgvAgent.Rows[0].Cells[1].Value.ToString();
                    SelEws = dgvAgent.Rows[0].Cells[5].Value.ToString();
                }
                else
                {
                    CommonMessageBox.ShowMessageWarning(message);
                }
            }
        }

        //private void SearchAgent()
        //{
        //    if (string.IsNullOrEmpty(txtAgentCode.Text) && !string.IsNullOrEmpty(txtAgentName.Text))
        //    {
        //        SqlCommand cmd = new SqlCommand();
        //        cmd.CommandText = "usp_GetAgentSearch";
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.Connection = sqlCon;
        //        cmd.Parameters.AddWithValue("@in_vchApplicationID", appId);
        //        //cmd.Parameters.AddWithValue("@in_vchAgentCode", txtAgentCode.Text);
        //        cmd.Parameters.AddWithValue("@in_vchAgentName", txtAgentName.Text);
        //        cmd.Parameters.AddWithValue("@in_vchDevice", device);
        //        cmd.Parameters.AddWithValue("@in_vchUserID", GlobalVariable.userName);
        //        try
        //        {
        //            sqlCon.Open();
        //            cmd.CommandTimeout = 120;
        //            SqlDataAdapter da = new SqlDataAdapter(cmd);
        //            dataset = new DataSet();
        //            da.Fill(dataset, "testGetAgentSearch");

        //            if (dataset.Tables[0].Rows.Count > 0)
        //            {
        //                string status = dataset.Tables[0].Rows[0]["Status"].ToString();
        //                string message = dataset.Tables[0].Rows[0]["Message"].ToString();
        //                if (status == "PASS")
        //                {

        //                    DataTable Agent_TB = CreateAgent_TB(dataset);
        //                    dgvAgent.DataSource = Agent_TB;
        //                    dgvAgent.Rows[0].Selected = true;
        //                    SelAgentCode = dgvAgent.Rows[0].Cells[0].Value.ToString();
        //                    SelAgentName = dgvAgent.Rows[0].Cells[1].Value.ToString();
        //                    SelEws = dgvAgent.Rows[0].Cells[5].Value.ToString();
        //                }
        //                else
        //                {
        //                    MessageBox.Show(message);
        //                }
        //            }
        //            else
        //            {
        //                dgvAgent.DataSource = null;
        //            }
        //        }
        //        catch (Exception objEx)
        //        {
        //            string str = objEx.Message;
        //            MessageBox.Show(str);
        //        }
        //        finally
        //        {
        //            if (sqlCon != null && sqlCon.State != ConnectionState.Closed)
        //            {
        //                sqlCon.Close();
        //            }
        //        }
        //    }
        //    else if (!string.IsNullOrEmpty(txtAgentCode.Text) && string.IsNullOrEmpty(txtAgentName.Text))
        //    {
        //        SqlCommand cmd = new SqlCommand();
        //        cmd.CommandText = "usp_GetAgentSearch";
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.Connection = sqlCon;
        //        cmd.Parameters.AddWithValue("@in_vchApplicationID", appId);
        //        cmd.Parameters.AddWithValue("@in_vchAgentCode", txtAgentCode.Text);
        //        //cmd.Parameters.AddWithValue("@in_vchAgentName", txtAgentName.Text);
        //        cmd.Parameters.AddWithValue("@in_vchDevice", device);
        //        cmd.Parameters.AddWithValue("@in_vchUserID", GlobalVariable.userName);
        //        try
        //        {
        //            sqlCon.Open();
        //            cmd.CommandTimeout = 120;
        //            SqlDataAdapter da = new SqlDataAdapter(cmd);
        //            dataset = new DataSet();
        //            da.Fill(dataset, "testGetAgentSearch");

        //            if (dataset.Tables[0].Rows.Count > 0)
        //            {
        //                string status = dataset.Tables[0].Rows[0]["Status"].ToString();
        //                string message = dataset.Tables[0].Rows[0]["Message"].ToString();
        //                if (status == "PASS")
        //                {

        //                    DataTable Agent_TB = CreateAgent_TB(dataset);
        //                    dgvAgent.DataSource = Agent_TB;
        //                    dgvAgent.Rows[0].Selected = true;
        //                    SelAgentCode = dgvAgent.Rows[0].Cells[0].Value.ToString();
        //                    SelAgentName = dgvAgent.Rows[0].Cells[1].Value.ToString();
        //                    SelEws = dgvAgent.Rows[0].Cells[5].Value.ToString();
        //                }
        //                else
        //                {
        //                    MessageBox.Show(message);
        //                }
        //            }
        //            else
        //            {
        //                dgvAgent.DataSource = null;
        //            }
        //        }
        //        catch (Exception objEx)
        //        {
        //            string str = objEx.Message;
        //            MessageBox.Show(str);
        //        }
        //        finally
        //        {
        //            if (sqlCon != null && sqlCon.State != ConnectionState.Closed)
        //            {
        //                sqlCon.Close();
        //            }
        //        }
        //    }
        //    else if (!string.IsNullOrEmpty(txtAgentCode.Text) && !string.IsNullOrEmpty(txtAgentName.Text))
        //    {
        //        SqlCommand cmd = new SqlCommand();
        //        cmd.CommandText = "usp_GetAgentSearch";
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.Connection = sqlCon;
        //        cmd.Parameters.AddWithValue("@in_vchApplicationID", appId);
        //        cmd.Parameters.AddWithValue("@in_vchAgentCode", txtAgentCode.Text);
        //        cmd.Parameters.AddWithValue("@in_vchAgentName", txtAgentName.Text);
        //        cmd.Parameters.AddWithValue("@in_vchDevice", device);
        //        cmd.Parameters.AddWithValue("@in_vchUserID", GlobalVariable.userName);
        //        try
        //        {
        //            sqlCon.Open();
        //            cmd.CommandTimeout = 120;
        //            SqlDataAdapter da = new SqlDataAdapter(cmd);
        //            dataset = new DataSet();
        //            da.Fill(dataset, "testGetAgentSearch");

        //            if (dataset.Tables[0].Rows.Count > 0)
        //            {
        //                string status = dataset.Tables[0].Rows[0]["Status"].ToString();
        //                string message = dataset.Tables[0].Rows[0]["Message"].ToString();
        //                if (status == "PASS")
        //                {

        //                    DataTable Agent_TB = CreateAgent_TB(dataset);
        //                    dgvAgent.DataSource = Agent_TB;
        //                    dgvAgent.Rows[0].Selected = true;
        //                    SelAgentCode = dgvAgent.Rows[0].Cells[0].Value.ToString();
        //                    SelAgentName = dgvAgent.Rows[0].Cells[1].Value.ToString();
        //                    SelEws = dgvAgent.Rows[0].Cells[5].Value.ToString();
        //                }
        //                else
        //                {
        //                    MessageBox.Show(message);
        //                }
        //            }
        //            else
        //            {
        //                dgvAgent.DataSource = null;
        //            }
        //        }
        //        catch (Exception objEx)
        //        {
        //            string str = objEx.Message;
        //            MessageBox.Show(str);
        //        }
        //        finally
        //        {
        //            if (sqlCon != null && sqlCon.State != ConnectionState.Closed)
        //            {
        //                sqlCon.Close();
        //            }
        //        }
        //    }
        //    else
        //    {
        //        SqlCommand cmd = new SqlCommand();
        //        cmd.CommandText = "usp_GetAgentSearch";
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        cmd.Connection = sqlCon;
        //        cmd.Parameters.AddWithValue("@in_vchApplicationID", appId);
        //        cmd.Parameters.AddWithValue("@in_vchAgentCode", txtAgentCode.Text);
        //        cmd.Parameters.AddWithValue("@in_vchAgentName", txtAgentName.Text);
        //        cmd.Parameters.AddWithValue("@in_vchDevice", device);
        //        cmd.Parameters.AddWithValue("@in_vchUserID", GlobalVariable.userName);
        //        try
        //        {
        //            sqlCon.Open();
        //            cmd.CommandTimeout = 120;
        //            SqlDataAdapter da = new SqlDataAdapter(cmd);
        //            dataset = new DataSet();
        //            da.Fill(dataset, "testGetAgentSearch");

        //            if (dataset.Tables[0].Rows.Count > 0)
        //            {
        //                string status = dataset.Tables[0].Rows[0]["Status"].ToString();
        //                string message = dataset.Tables[0].Rows[0]["Message"].ToString();
        //                if (status == "PASS")
        //                {

        //                    DataTable Agent_TB = CreateAgent_TB(dataset);
        //                    dgvAgent.DataSource = Agent_TB;
        //                    dgvAgent.Rows[0].Selected = true;
        //                    SelAgentCode = dgvAgent.Rows[0].Cells[0].Value.ToString();
        //                    SelAgentName = dgvAgent.Rows[0].Cells[1].Value.ToString();
        //                    SelEws = dgvAgent.Rows[0].Cells[5].Value.ToString();
        //                }
        //                else
        //                {
        //                    MessageBox.Show(message);
        //                }
        //            }

        //            else
        //            {
        //                dgvAgent.DataSource = null;
        //            }
        //        }
        //        catch (Exception objEx)
        //        {
        //            string str = objEx.Message;
        //            MessageBox.Show(str);
        //        }
        //        finally
        //        {
        //            if (sqlCon != null && sqlCon.State != ConnectionState.Closed)
        //            {
        //                sqlCon.Close();
        //            }
        //        }
        //    }
        //}
        #endregion


    }
}
